package com.taptech.doufu.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.taptech.doufu.ui.adapter.CommonViewPagerAdapter;
import com.taptech.doufu.ui.view.TTHomeViewPager;
import com.taptech.doufu.ui.dialog.CpAddContentDialog;
import com.taptech.doufu.R;
import com.taptech.doufu.bean.UserWorksBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.services.personalcenter.PersonalInfoService;
import com.taptech.doufu.ui.view.TopTabHostViewGroup;
import com.taptech.doufu.util.DisplayUtil;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.ui.view.WaitDialog;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

public class NewMyCpFragment extends ScrollAbleFragment implements HttpResponseListener, View.OnClickListener
{

    private TopTabHostViewGroup mTabView;
    private List<Fragment> mSweepListViews;
    private TTHomeViewPager mViewPager;
    private CommonViewPagerAdapter viewPagerAdapter;
    private final String NAMESTRING_CP[] = new String[]{"我的订阅", "小说", "绘画", "日常"};
    public final static int TYPE_CP_MY_ORDER = 1;
    public final static int TYPE_CP_NOTE = 2;
    public final static int TYPE_CP_NOVEL = 3;
    public final static int TYPE_CP_DRAW = 4;
    public final static int TYPE_PERSONAL_PUBLISHED_NOTE = 5;
    public final static int TYPE_PERSONAL_PUBLISHED_SWEEP = 6;
    public final static int TYPE_PERSONAL_PUBLISHED_UGC = 7;
    public final static int TYPE_PERSONAL_PUBLISHED_COS = 8;
    private final int ALL_CP_TYPE[] = new int[]{TYPE_CP_MY_ORDER, TYPE_CP_NOVEL, TYPE_CP_DRAW, TYPE_CP_NOTE};
    public static final int ALL_PERSONAL_PUBLISHED_TYPE[] = new int[]{TYPE_PERSONAL_PUBLISHED_NOTE, TYPE_PERSONAL_PUBLISHED_SWEEP, TYPE_PERSONAL_PUBLISHED_UGC, TYPE_PERSONAL_PUBLISHED_COS};
    int current_type;
    public final static int TYPE_FROM_CP = 1001;
    public final static int TYPE_FROM_PERSONAL_PUBLISHED = 1002;
    public final static String LOAD_LATER = "load_later";
    public static final String ARGUMENT = "argument";
    private View rootView;
    private WaitDialog dialog;
    private ImageView add_content_iv;
    private boolean isLoadLater = false;

    public static NewMyCpFragment newInstance(int type)
    {
        Bundle args = new Bundle();
        args.putInt(ARGUMENT, type);
        NewMyCpFragment fragment = new NewMyCpFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static NewMyCpFragment newInstance(int type,boolean isLoadLater)
    {
        Bundle args = new Bundle();
        args.putInt(ARGUMENT, type);
        args.putBoolean(LOAD_LATER,isLoadLater);
        NewMyCpFragment fragment = new NewMyCpFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onResume(String name, Context context)
    {
        super.onResume("NewMyCpFragment", context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null)
        {
            current_type = bundle.getInt(ARGUMENT, 1);
            isLoadLater = bundle.getBoolean(LOAD_LATER);
        }
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        rootView = inflater.inflate(R.layout.fragment_my_cp, container, false);
        initView(rootView);
        return rootView;
    }

    private void loadData()
    {
        dialog = new WaitDialog(getActivity(), R.style.updateDialog);
        dialog.show();
        if (AccountService.getInstance().getUserUid() != null)
        {
            String userId = AccountService.getInstance().getBaseAccount().getUid();
            PersonalInfoService.getInstance().getUserWorksRequest(this, userId);
        }
    }

    protected void initView(View parent)
    {
        add_content_iv = (ImageView) parent.findViewById(R.id.add_content_iv);
        add_content_iv.setOnClickListener(this);
        mTabView = (TopTabHostViewGroup) parent.findViewById(R.id.top_tab_view);
        mTabView.setChoosePageListener(new TopTabHostViewGroup.ChoosePage()
        {
            @Override
            public void updatePage(int position)
            {
                mViewPager.setCurrentItem(position, true);
                //setAddView(position);
            }
        });
        mViewPager = (TTHomeViewPager) parent.findViewById(R.id.sweep_view_pager);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {

            @Override
            public void onPageSelected(int arg0)
            {
                // TODO Auto-generated method stub
                mTabView.moveTopStripTo(arg0);
                //setAddView(arg0);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2)
            {
                // TODO Auto-generated method stub
            }

            @Override
            public void onPageScrollStateChanged(int arg0)
            {
                // TODO Auto-generated method stub
            }

        });
        if (!isLoadLater) {
            load();
        }
    }


    public void loadLater() {
        if (isLoadLater) {
            load();
            isLoadLater = false;
        }
    }


    private void load() {
        switch (current_type)
        {
            case TYPE_FROM_CP:
                mTabView.putStringsTabs(NAMESTRING_CP);
                add_content_iv.setVisibility(View.GONE);
                mViewPager.setOffscreenPageLimit(2);
                initFragmentList();
                viewPagerAdapter = new CommonViewPagerAdapter(getFragmentManager(), mSweepListViews);
                mViewPager.setAdapter(viewPagerAdapter);
                break;
            case TYPE_FROM_PERSONAL_PUBLISHED:
                mViewPager.setOffscreenPageLimit(1);
                loadData();
                break;
        }
    }


    private void initFragmentList()
    {
        mSweepListViews = new ArrayList<Fragment>();
        switch (current_type)
        {
            case TYPE_FROM_CP:
                for (int i = 0; i < NAMESTRING_CP.length; i++)
                {
                    if (i == 0)
                    {
                        mSweepListViews.add(MyOrderCpFragment.newInstance(MyOrderCpFragment.TYPE_MY_CP));
                    } else
                    {
                        mSweepListViews.add(CommonCpFragment.newInstance(ALL_CP_TYPE[i], AccountService.getInstance().getUserUid()));
                    }
                }
                break;
            case TYPE_FROM_PERSONAL_PUBLISHED:
                for (int i = 0; i < ALL_PERSONAL_PUBLISHED_TYPE.length; i++)
                {
                    mSweepListViews.add(CommonCpFragment.newInstance(ALL_PERSONAL_PUBLISHED_TYPE[i], AccountService.getInstance().getUserUid()));
                }
                break;
        }

    }

    @Override
    public void handleResponse(int handleType, HttpResponseObject response)
    {
        dialog.cancel();
        if (response == null)
        {
            UIUtil.toastMessage(getActivity(), Constant.loadingFail);
            return;
        }
        if (response.getStatus() == 0)
        {
            switch (handleType)
            {
                case PersonalInfoService.HANDLE_HAS_WORKS:
                    JSONObject data = (JSONObject) response.getData();
                    UserWorksBean userWorksBean = new UserWorksBean();
                    userWorksBean.setJson2(data);
                    initUpView(userWorksBean);
                    break;
            }

        } else
        {
            if (response.getUser_msg() != null && !response.getUser_msg().isEmpty())
            {
                UIUtil.toastMessage(getActivity(), response.getUser_msg());
            } else
            {
                UIUtil.toastMessage(getActivity(), Constant.loadingFail);
            }
        }
    }

    protected void initUpView(UserWorksBean userWorksBean)
    {
        if (userWorksBean == null)
        {
            return;
        }
        mSweepListViews = new ArrayList<>();
        mSweepListViews.add(CommonCpFragment.newInstance(NewMyCpFragment.TYPE_PERSONAL_PUBLISHED_NOTE, AccountService.getInstance().getUserUid()));
        mSweepListViews.add(CommonCpFragment.newInstance(NewMyCpFragment.TYPE_PERSONAL_PUBLISHED_SWEEP, AccountService.getInstance().getUserUid()));
        mSweepListViews.add(CommonCpFragment.newInstance(CommonCpFragment.TYPE_PERSONAL_DRAW, AccountService.getInstance().getUserUid(),false));
        List<String> name_lists = new ArrayList<String>();
        name_lists.add("日常");
        name_lists.add("文评");
        name_lists.add("绘画");
        if (Integer.parseInt(userWorksBean.getTopic_count()) > 0)
        {
            name_lists.add("话题");
            mSweepListViews.add(CommonCpFragment.newInstance(NewMyCpFragment.TYPE_PERSONAL_PUBLISHED_UGC, AccountService.getInstance().getUserUid()));
        }
        if (Integer.parseInt(userWorksBean.getCosplay_count()) > 0)
        {
            name_lists.add("COS");
            mSweepListViews.add(CommonCpFragment.newInstance(NewMyCpFragment.TYPE_PERSONAL_PUBLISHED_COS, AccountService.getInstance().getUserUid()));
        }
        String[] NAMESTRING = (String[]) name_lists.toArray(new String[name_lists.size()]);
        mTabView.putStringsTabs(NAMESTRING);
        viewPagerAdapter = new CommonViewPagerAdapter(getFragmentManager(), mSweepListViews);
        mViewPager.setAdapter(viewPagerAdapter);
    }

    @Override
    public View getScrollableView()
    {
        return rootView;
    }

    @Override
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.add_content_iv:
                DisplayUtil.makeAddContentPopupWindow(getActivity(), CpAddContentDialog.TYPE_FRAGMENT);
                break;
        }
    }
}